﻿using Gtkj.CMS.BLL;
using Gtkj.CMS.Model;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Aspose.Cells;
using System.IO;

namespace Gtkj.CMS.App.Controllers
{
    public class HomeController : Controller
    {
        AdminInfoService userservice = new AdminInfoService();
        OrderService orders = new OrderService();
        CustomerService cservice = new CustomerService();
        VisitService vservice = new VisitService();
        SummaryService ssv = new SummaryService();
        string APPJSON = "application/json";
        protected int worlduserid;

        public HomeController()
        {

        }
        // GET: Home
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Login(string UserName, string PassWord)
        {
            if (string.IsNullOrEmpty(UserName))
            {
                return Content(JsonConvert.SerializeObject(new { success = false, message = "账号不能为空！" }), APPJSON);
            }
            if (string.IsNullOrEmpty(PassWord))
            {
                return Content(JsonConvert.SerializeObject(new { success = false, message = "密码不能为空！" }), APPJSON);
            }
            AdminInfo model = userservice.LoadEntities(e => e.Account == UserName).FirstOrDefault();

            if (model == null)
            {
                return Content(JsonConvert.SerializeObject(new { success = false, message = "用户不存在！" }), APPJSON);
            }
            if (model.PassWord != PassWord)
            {
                return Content(JsonConvert.SerializeObject(new { success = false, message = "密码错误！" }), APPJSON);
            }
            if (model.State != 1)
            {
                return Content(JsonConvert.SerializeObject(new { success = false, message = "用户状态异常！" }), APPJSON);
            }
            Session["User"] = model;
            Session["UserId"] = model.Id;
            model.LastLoginTime = DateTime.Now;
            worlduserid = model.Id;
            userservice.EditEntity(model);
            return Content(JsonConvert.SerializeObject(new { success = true, Url = "/Home/Main" }), APPJSON);

        }



        public ActionResult Main()
        {
            if (Session["User"] == null)
            {
                return View("Index");
            }
            #region 数字
            var modela = (AdminInfo)Session["User"];
            var n = DateTime.Now.Date;
            if (modela.IsAdmin == true)
            {
                ViewData["jinridingdan"] = orders.LoadEntities(e => e.CreateTime.Year == n.Year && e.CreateTime.Month == n.Month && e.CreateTime.Day == n.Day).Count();

                ViewData["jinri"] = orders.LoadEntities(e => e.VisitTime == n).Count();
                n = n.AddDays(-3);
                ViewData["3ri"] = orders.LoadEntities(e => e.VisitTime < n).Count();
                n = n.AddDays(-4);
                ViewData["7ri"] = orders.LoadEntities(e => e.VisitTime < n).Count();
                n = n.AddDays(-7);
                ViewData["15ri"] = orders.LoadEntities(e => e.VisitTime < n).Count();
            }
            else
            {
                ViewData["jinridingdan"] = orders.LoadEntities(e => e.CreateUserId == modela.Id && e.CreateTime.Year == n.Year && e.CreateTime.Month == n.Month && e.CreateTime.Day == n.Day).Count();
                ViewData["jinri"] = orders.LoadEntities(e => e.VisitTime == n && e.CreateUserId == modela.Id).Count();
                n = n.AddDays(-3);
                ViewData["3ri"] = orders.LoadEntities(e => e.VisitTime < n && e.CreateUserId == modela.Id).Count();
                n = n.AddDays(-4);
                ViewData["7ri"] = orders.LoadEntities(e => e.VisitTime < n && e.CreateUserId == modela.Id).Count();
                n = n.AddDays(-7);
                ViewData["15ri"] = orders.LoadEntities(e => e.VisitTime < n && e.CreateUserId == modela.Id).Count();
            }
            ViewData["User"] = modela;
            #endregion
            return View();
        }

        public ActionResult Summary()
        {
            if (Session["User"] == null)
            {
                return View("Index");
            }
            #region 数字
            var modela = (AdminInfo)Session["User"];
            var n = DateTime.Now.Date;
            if (modela.IsAdmin == true)
            {
                ViewData["jinridingdan"] = orders.LoadEntities(e => e.CreateTime.Year == n.Year && e.CreateTime.Month == n.Month && e.CreateTime.Day == n.Day).Count();
                ViewData["jinri"] = orders.LoadEntities(e => e.VisitTime == n).Count();
                n = n.AddDays(-3);
                ViewData["3ri"] = orders.LoadEntities(e => e.VisitTime < n).Count();
                n = n.AddDays(-4);
                ViewData["7ri"] = orders.LoadEntities(e => e.VisitTime < n).Count();
                n = n.AddDays(-7);
                ViewData["15ri"] = orders.LoadEntities(e => e.VisitTime < n).Count();
            }
            else
            {
                ViewData["jinridingdan"] = orders.LoadEntities(e => e.CreateUserId == modela.Id && e.CreateTime.Year == n.Year && e.CreateTime.Month == n.Month && e.CreateTime.Day == n.Day).Count();
                ViewData["jinri"] = orders.LoadEntities(e => e.VisitTime == n && e.CreateUserId == modela.Id).Count();
                n = n.AddDays(-3);
                ViewData["3ri"] = orders.LoadEntities(e => e.VisitTime < n && e.CreateUserId == modela.Id).Count();
                n = n.AddDays(-4);
                ViewData["7ri"] = orders.LoadEntities(e => e.VisitTime < n && e.CreateUserId == modela.Id).Count();
                n = n.AddDays(-7);
                ViewData["15ri"] = orders.LoadEntities(e => e.VisitTime < n && e.CreateUserId == modela.Id).Count();
            }
            ViewData["User"] = modela;
            #endregion
            ViewData["IsAdmin"] = modela.IsAdmin;
            List<AdminInfo> users = userservice.LoadEntities(e => true).ToList();
            ViewData["Users"] = users;
            return View();
        }

        public ActionResult SummaryEdit()
        {
            AdminInfo user = (AdminInfo)Session["User"];
            if (user == null)
            {
                return View("Index");
            }
            #region 数字
            var modela = (AdminInfo)Session["User"];
            var n = DateTime.Now.Date;
            if (modela.IsAdmin == true)
            {
                ViewData["jinridingdan"] = orders.LoadEntities(e => e.CreateTime.Year == n.Year && e.CreateTime.Month == n.Month && e.CreateTime.Day == n.Day).Count();
                ViewData["jinri"] = orders.LoadEntities(e => e.VisitTime == n).Count();
                n = n.AddDays(-3);
                ViewData["3ri"] = orders.LoadEntities(e => e.VisitTime < n).Count();
                n = n.AddDays(-4);
                ViewData["7ri"] = orders.LoadEntities(e => e.VisitTime < n).Count();
                n = n.AddDays(-7);
                ViewData["15ri"] = orders.LoadEntities(e => e.VisitTime < n).Count();
            }
            else
            {
                ViewData["jinridingdan"] = orders.LoadEntities(e => e.CreateUserId == modela.Id && e.CreateTime.Year == n.Year && e.CreateTime.Month == n.Month && e.CreateTime.Day == n.Day).Count();
                ViewData["jinri"] = orders.LoadEntities(e => e.VisitTime == n && e.CreateUserId == modela.Id).Count();
                n = n.AddDays(-3);
                ViewData["3ri"] = orders.LoadEntities(e => e.VisitTime < n && e.CreateUserId == modela.Id).Count();
                n = n.AddDays(-4);
                ViewData["7ri"] = orders.LoadEntities(e => e.VisitTime < n && e.CreateUserId == modela.Id).Count();
                n = n.AddDays(-7);
                ViewData["15ri"] = orders.LoadEntities(e => e.VisitTime < n && e.CreateUserId == modela.Id).Count();
            }
            ViewData["User"] = modela;
            #endregion

            DateTime now = DateTime.Now;
            var model = ssv.LoadEntities(e => e.CreateUserId == user.Id && e.CreateTime.Day == now.Day && e.CreateTime.Month == now.Month).FirstOrDefault();
            now = now.AddDays(-1);
            var model2 = ssv.LoadEntities(e => e.CreateUserId == user.Id && e.CreateTime.Day == now.Day && e.CreateTime.Month == now.Month).FirstOrDefault();

            ViewData["LastDaySumNum"] = model2 == null ? 0 : model2.TodaySumNum;
            ViewData["LastDaySummaryId"] = model2 == null ? 0 : model2.Id;
            if (model != null)
            {
                ViewData["MyDate"] = model.CreateTime.ToString("yyyy-MM-dd");
                return View(model);
            }
            ViewData["MyDate"] = DateTime.Now.ToString("yyyy-MM-dd");
            return View(new Summary() { });
        }

        [HttpPost]
        public ActionResult AddSummaryOn(Summary model)
        {
            int lastid;
            try
            {
                if (int.TryParse(Request["LastDaySummaryId"], out lastid) && lastid != 0)
                {
                    var lastmodel = ssv.LoadEntities(e => e.Id == lastid).FirstOrDefault();
                    if (lastmodel != null && !string.IsNullOrEmpty(Request["LastDaySumNum"]))
                    {
                        lastmodel.TodaySumNum = Convert.ToInt32(Request["LastDaySumNum"]);
                        ssv.EditEntity(lastmodel);
                    }
                }
                if (model.Id != 0)
                {
                    if (ssv.EditEntity(model))
                    {
                        return Content(JsonConvert.SerializeObject(new { success = true, msg = "！" }), APPJSON);
                    }
                    else
                    {
                        return Content(JsonConvert.SerializeObject(new { success = false, msg = "修改失败！" }), APPJSON);
                    }
                }
                else
                {
                    int id = Convert.ToInt32(Session["UserId"]);
                    var now = DateTime.Now.Date;
                    var tommorrow = now.AddDays(1);
                    if (ssv.LoadEntities(e => e.CreateUserId == id && e.CreateTime >= now && e.CreateTime <= tommorrow).Count() > 0)
                    {
                        return Content(JsonConvert.SerializeObject(new { success = false, msg = "今天已经汇总过了！" }), APPJSON);
                    }
                    model.CreateUserId = Convert.ToInt32(Session["UserId"]);
                    model.CreateTime = DateTime.Now;
                    if (Request["CreateTime"] != null)
                    {
                        model.CreateTime = Convert.ToDateTime(Request["CreateTime"]);
                    }
                    else
                    {
                        model.CreateTime = DateTime.Now;
                    }
                    ssv.AddEntity(model);
                    return Content(JsonConvert.SerializeObject(new { success = true, msg = "！" }), APPJSON);
                }


            }
            catch (Exception ex)
            {
                return Content(JsonConvert.SerializeObject(new { success = false, msg = "操作失败！" }), APPJSON);
            }
        }


        public ActionResult ExportExcelSummary()
        {
            try
            {
                var temp = userservice.LoadEntities(e => e.IsAdmin == false);
                var pagesize = 10;

                var totalcount = temp.Count();
                var pagecount = (totalcount + pagesize - 1) / pagesize;
                int watchtype;
                if (!int.TryParse(Request["WatchType"], out watchtype))
                {
                    watchtype = 0;
                }
                DateTime d;
                if (!DateTime.TryParse(Request["CreateTime"], out d))
                {
                    d = DateTime.Now;
                }
                DateTime endtime;
                if (!DateTime.TryParse(Request["EndTime"], out endtime))
                {
                    endtime = DateTime.Now;
                }
                List<SummaryViewModel> list = GetSummayPageList(watchtype, d, endtime, temp.ToList());


                Workbook workbook = new Workbook();
                Worksheet sheet = workbook.Worksheets[0];
                Cells cells = sheet.Cells;//单元格

                //为标题设置样式    
                Style styleTitle = workbook.Styles[workbook.Styles.Add()];//新增样式
                styleTitle.HorizontalAlignment = TextAlignmentType.Center;//文字居中
                styleTitle.Font.Name = "宋体";//文字字体
                styleTitle.Font.Size = 18;//文字大小
                styleTitle.Font.IsBold = true;//粗体

                //样式2
                Style style2 = workbook.Styles[workbook.Styles.Add()];//新增样式
                style2.HorizontalAlignment = TextAlignmentType.Center;//文字居中
                style2.Font.Name = "宋体";//文字字体
                style2.Font.Size = 14;//文字大小
                style2.Font.IsBold = true;//粗体
                style2.IsTextWrapped = true;//单元格内容自动换行

                //样式3
                Style style3 = workbook.Styles[workbook.Styles.Add()];//新增样式
                style3.HorizontalAlignment = TextAlignmentType.Center;//文字居中
                style3.Font.Name = "宋体";//文字字体
                style3.Font.Size = 12;//文字大小

                int Colnum = 15;//表格列数
                int Rownum = list.Count;//表格行数

                //生成行1 标题行   
                cells.Merge(0, 0, 1, Colnum);//合并单元格
                cells[0, 0].PutValue("汇总报告");//填写内容
                cells[0, 0].SetStyle(styleTitle);
                cells.SetRowHeight(0, 45);

                cells[1, 0].PutValue("序号");
                cells[1, 0].SetStyle(style2);
                cells.SetColumnWidth(0, 3);

                cells[1, 1].PutValue("员工");
                cells[1, 1].SetStyle(style2);
                cells.SetColumnWidth(0, 3);

                cells[1, 2].PutValue("加粉数");
                cells[1, 2].SetStyle(style2);
                cells.SetColumnWidth(1, 30);

                cells[1, 3].PutValue("对话数");
                cells[1, 3].SetStyle(style2);
                cells.SetColumnWidth(2, 5);

                cells[1, 4].PutValue("新粉出单");
                cells[1, 4].SetStyle(style2);
                cells.SetColumnWidth(3, 5);

                cells[1, 5].PutValue("老粉出单");
                cells[1, 5].SetStyle(style2);
                cells.SetColumnWidth(3, 10);


                cells[1, 6].PutValue("体验装出单");
                cells[1, 6].SetStyle(style2);
                cells.SetColumnWidth(4, 15);

                cells[1, 7].PutValue("体验装转正");
                cells[1, 7].SetStyle(style2);
                cells.SetColumnWidth(5, 100);


                cells[1, 8].PutValue("复购");
                cells[1, 8].SetStyle(style2);
                cells.SetColumnWidth(6, 15);

                cells[1, 9].PutValue("新粉转化率");
                cells[1, 9].SetStyle(style2);
                cells.SetColumnWidth(7, 15);

                cells[1, 10].PutValue("体验装转化率");
                cells[1, 10].SetStyle(style2);
                cells.SetColumnWidth(8, 15);

                cells[1, 11].PutValue("转正率");
                cells[1, 11].SetStyle(style2);
                cells.SetColumnWidth(9, 15);

                cells[1, 12].PutValue("复购率");
                cells[1, 12].SetStyle(style2);
                cells.SetColumnWidth(10, 25);

                cells[1, 13].PutValue("老粉转化率");
                cells[1, 13].SetStyle(style2);
                cells.SetColumnWidth(10, 25);

                cells[1, 14].PutValue("整体转化率");
                cells[1, 14].SetStyle(style2);
                cells.SetColumnWidth(10, 25);

                ////生成数据行
                for (int i = 0; i < Rownum; i++)
                {
                    cells[2 + i, 0].PutValue(i + 1);
                    cells[2 + i, 0].SetStyle(style3);

                    cells[2 + i, 1].PutValue(list[i].UserName);
                    cells[2 + i, 1].SetStyle(style3);

                    cells[2 + i, 2].PutValue(list[i].TodayFansNum);
                    cells[2 + i, 2].SetStyle(style3);

                    cells[2 + i, 3].PutValue(list[i].TodayTalkNum);
                    cells[2 + i, 3].SetStyle(style3);

                    cells[2 + i, 4].PutValue(list[i].NewFansOrderNum);
                    cells[2 + i, 4].SetStyle(style3);

                    cells[2 + i, 5].PutValue(list[i].OldFansOrderNum);
                    cells[2 + i, 5].SetStyle(style3);

                    cells[2 + i, 6].PutValue(list[i].SampleOrderNum);
                    cells[2 + i, 6].SetStyle(style3);

                    cells[2 + i, 7].PutValue(list[i].SampleToforMalOrderNum);
                    cells[2 + i, 7].SetStyle(style3);

                    cells[2 + i, 8].PutValue(list[i].RePurchaseOrderNum);
                    cells[2 + i, 8].SetStyle(style3);

                    cells[2 + i, 9].PutValue(list[i].xfzhl);
                    cells[2 + i, 9].SetStyle(style3);

                    cells[2 + i, 10].PutValue(list[i].tyzzhl);
                    cells[2 + i, 10].SetStyle(style3);

                    cells[2 + i, 11].PutValue(list[i].zzl);
                    cells[2 + i, 11].SetStyle(style3);

                    cells[2 + i, 12].PutValue(list[i].fgl);
                    cells[2 + i, 12].SetStyle(style3);

                    cells[2 + i, 13].PutValue(list[i].lfzhl);
                    cells[2 + i, 13].SetStyle(style3);

                    cells[2 + i, 14].PutValue(list[i].ztzhl);
                    cells[2 + i, 14].SetStyle(style3);

                    cells.SetRowHeight(3 + i, 24);
                }
                string filename = DateTime.Now.ToString("yyyyMMddhhmmss");
                filename = "/ExportDoc/" + filename + ".xls";
                workbook.Save(Server.MapPath(filename));
                return Content(JsonConvert.SerializeObject(new { success = true, url = filename }), APPJSON);
            }
            catch (Exception ex)
            {
                return Content(JsonConvert.SerializeObject(new { success = false, msg = "数据异常" }), APPJSON);
            }
        }

        public ActionResult DeleteCustomer()
        {
            try
            {
                int CustomerId;
                if (!int.TryParse(Request["CustomerId"], out CustomerId))
                {
                    return Content(JsonConvert.SerializeObject(new { success = false, msg = "数据异常！" }), APPJSON);
                }
                var order = cservice.LoadEntities(e => e.Id == CustomerId).FirstOrDefault();
                if (order == null)
                {
                    return Content(JsonConvert.SerializeObject(new { success = false, msg = "该用户已被删除！" }), APPJSON);
                }
                if (cservice.DeleteEntity(order))
                {
                    return Content(JsonConvert.SerializeObject(new { success = true, msg = "！" }), APPJSON);
                }
                return Content(JsonConvert.SerializeObject(new { success = false, msg = "删除失败！" }), APPJSON);
            }
            catch (Exception ex)
            {
                return Content(JsonConvert.SerializeObject(new { success = false, msg = "删除失败！" }), APPJSON);
            }
        }

        [HttpPost]
        public ActionResult GetSummaryPage()
        {
            var pageindex = 1;
            if (!int.TryParse(Request["pageindex"], out pageindex))
            {
                return Content(JsonConvert.SerializeObject(new { success = false }), APPJSON);
            }

            var temp = userservice.LoadEntities(e => e.IsAdmin == false);
            var pagesize = 10;

            var totalcount = temp.Count();
            var pagecount = (totalcount + pagesize - 1) / pagesize;
            int watchtype;
            if (!int.TryParse(Request["WatchType"], out watchtype))
            {
                watchtype = 0;
            }
            DateTime starttime;
            if (!DateTime.TryParse(Request["CreateTime"], out starttime))
            {
                starttime = DateTime.Now;
            }
            DateTime endtime;
            if (!DateTime.TryParse(Request["EndTime"], out endtime))
            {
                endtime = DateTime.Now;
            }
            var list = GetSummayPageList(watchtype, starttime, endtime, temp.OrderBy(e => e.Id).Skip((pageindex - 1) * pagesize).Take(pagesize).ToList());
            return Content(JsonConvert.SerializeObject(new { success = true, totalcount = totalcount, list = list, pagecount = pagecount }), APPJSON);

        }

        private List<SummaryViewModel> GetSummayPageList(int watchtype, DateTime now, DateTime endtime, List<AdminInfo> temp)
        {
            List<SummaryViewModel> list = new List<SummaryViewModel>();
            foreach (var item in temp)
            {
                // var todaysummary = item.Summary.Where(e => e.CreateTime >= now && e.CreateTime < endtime.AddDays(1).Date).FirstOrDefault();
                //var todaysummary = item.Summary.Where(e => e.CreateTime>=now.Date&&e.CreateTime<now.Date.AddDays(1)).FirstOrDefault();
                var svm = new SummaryViewModel();
                svm.UserName = item.UserName;
                if (true)
                {
                }
                //svm.TodayFansNum = todaysummary.TodayFansNum.ToString();
                //svm.TodayTalkNum = todaysummary.TodayTalkNum.ToString();
                //svm.NewFansOrderNum = todaysummary.NewFansOrderNum.ToString();
                //svm.OldFansOrderNum = todaysummary.OldFansOrderNum.ToString();
                //svm.SampleOrderNum = todaysummary.SampleOrderNum.ToString();
                //svm.SampleToforMalOrderNum = todaysummary.SampleToForMalOrderNum.ToString();
                //svm.RePurchaseOrderNum = todaysummary.RePurchaseOrderNum.ToString();
                //svm.TodaySumNum = todaysummary.TodaySumNum.ToString();
                svm.TodayFansNum = item.Summary.Where(e => e.CreateTime >= now.Date && e.CreateTime < endtime.Date.AddDays(1)).Sum(e => e.TodayFansNum).ToString();
                svm.TodayTalkNum = item.Summary.Where(e => e.CreateTime >= now.Date && e.CreateTime < endtime.Date.AddDays(1)).Sum(e => e.TodayTalkNum).ToString();
                svm.NewFansOrderNum = item.Summary.Where(e => e.CreateTime >= now.Date && e.CreateTime < endtime.Date.AddDays(1)).Sum(e => e.NewFansOrderNum).ToString();
                svm.OldFansOrderNum = item.Summary.Where(e => e.CreateTime >= now.Date && e.CreateTime < endtime.Date.AddDays(1)).Sum(e => e.OldFansOrderNum).ToString();
                svm.SampleOrderNum = item.Summary.Where(e => e.CreateTime >= now.Date && e.CreateTime < endtime.Date.AddDays(1)).Sum(e => e.SampleOrderNum).ToString();
                svm.SampleToforMalOrderNum = item.Summary.Where(e => e.CreateTime >= now.Date && e.CreateTime < endtime.Date.AddDays(1)).Sum(e => e.SampleToForMalOrderNum).ToString();
                svm.RePurchaseOrderNum = item.Summary.Where(e => e.CreateTime >= now.Date && e.CreateTime < endtime.Date.AddDays(1)).Sum(e => e.RePurchaseOrderNum).ToString();
                svm.TodaySumNum = item.Summary.Where(e => e.CreateTime >= now.Date && e.CreateTime < endtime.Date.AddDays(1)).Sum(e => e.TodaySumNum).ToString();

                var slist = item.Summary;
                //DateTime maxdate = DateTime.Now.Date.AddDays(1);
                //DateTime minidate = DateTime.Now;
                //switch (watchtype)
                //{
                //    case 0:
                //        minidate = Convert.ToDateTime("1900-1-1");
                //        break;
                //    case 1:
                //        minidate = DateTime.Now.Date;
                //        break;
                //    case 2:
                //        minidate = DateTime.Now.Date.AddDays(-7);
                //        break;
                //    case 3:
                //        minidate = DateTime.Now.Date.AddDays(-30);
                //        break;
                //    case 4:
                //        minidate = DateTime.Now.Date.AddDays(-90);
                //        break;
                //}
                slist = slist.Where(e => e.CreateTime >= now.Date && e.CreateTime < endtime.Date.AddDays(1)).ToList();
                if (slist.Count > 0)
                {
                    svm.xfzhl = ToBaifenbi(slist.Sum(e => e.NewFansOrderNum), slist.Sum(e => e.TodayFansNum));
                    svm.tyzzhl = ToBaifenbi(slist.Sum(e => e.SampleOrderNum), slist.Sum(e => e.TodayFansNum));
                    svm.zzl = ToBaifenbi(slist.Sum(e => e.SampleToForMalOrderNum), slist.Sum(e => e.SampleOrderNum));
                    svm.fgl = ToBaifenbi(slist.Sum(e => e.RePurchaseOrderNum),
                        (
                           slist.Sum(e => e.OldFansOrderNum + e.NewFansOrderNum + e.SampleToForMalOrderNum)
                        ));
                    svm.lfzhl = ToBaifenbi(slist.Sum(e => e.OldFansOrderNum), slist.Sum(e => e.TodayFansNum));
                    svm.ztzhl = ToBaifenbi(
                        (slist.Sum(e => e.NewFansOrderNum + e.OldFansOrderNum + e.SampleToForMalOrderNum)),
                        (slist.Sum(e => e.TodayFansNum)));
                }
                list.Add(svm);
            }
            return list;
        }


        public string ToBaifenbi(object a, object b)
        {
            double rate = new double();
            if ((int)a==0||(int)b==0)
            {
                return "-";
            }
            try
            {
                rate = Convert.ToDouble(a) / Convert.ToDouble(b);
            }
            catch (Exception ex)
            {
                return "";
            }
            return rate.ToString("p"); //格式为12.23%

        }


        public ActionResult Exit()
        {
            Session.Clear();
            return View("Index");
        }

        public ActionResult GetOrder(int type = 0)
        {
            if (Session["User"] == null)
            {
                return View("Index");
            }
            #region 数字
            var modela = (AdminInfo)Session["User"];
            var n = DateTime.Now.Date;
            ViewData["MoneySum"] = 0;
            if (modela.IsAdmin == true)
            {
                if (orders.LoadEntities(e => e.CreateTime.Year == n.Year && e.CreateTime.Month == n.Month && e.CreateTime.Day == n.Day).Count() > 0)
                {
                    ViewData["MoneySum"] = orders.LoadEntities(e => e.CreateTime.Year == n.Year && e.CreateTime.Month == n.Month && e.CreateTime.Day == n.Day).ToList().Sum(e => e.Price).ToString("C");
                }
            }
            else
            {

                if (orders.LoadEntities(e => e.CreateTime.Year == n.Year && e.CreateTime.Month == n.Month && e.CreateTime.Day == n.Day).Count() > 0)
                {
                    ViewData["MoneySum"] = orders.LoadEntities(e => e.CreateTime.Year == n.Year && e.CreateTime.Month == n.Month && e.CreateTime.Day == n.Day && e.CreateUserId == modela.Id).ToList().Sum(e => e.Price).ToString("C");
                }
            }
            if (modela.IsAdmin == true)
            {
                ViewData["jinridingdan"] = orders.LoadEntities(e => e.CreateTime.Year == n.Year && e.CreateTime.Month == n.Month && e.CreateTime.Day == n.Day).Count();
                ViewData["jinri"] = orders.LoadEntities(e => e.VisitTime == n).Count();
                n = n.AddDays(-3);
                ViewData["3ri"] = orders.LoadEntities(e => e.VisitTime < n).Count();
                n = n.AddDays(-4);
                ViewData["7ri"] = orders.LoadEntities(e => e.VisitTime < n).Count();
                n = n.AddDays(-7);
                ViewData["15ri"] = orders.LoadEntities(e => e.VisitTime < n).Count();
            }
            else
            {
                ViewData["jinridingdan"] = orders.LoadEntities(e => e.CreateUserId == modela.Id && e.CreateTime.Year == n.Year && e.CreateTime.Month == n.Month && e.CreateTime.Day == n.Day).Count();
                ViewData["jinri"] = orders.LoadEntities(e => e.VisitTime == n && e.CreateUserId == modela.Id).ToList().Count;
                n = n.AddDays(-3);
                ViewData["3ri"] = orders.LoadEntities(e => e.VisitTime < n && e.CreateUserId == modela.Id).ToList().Count;
                n = n.AddDays(-4);
                ViewData["7ri"] = orders.LoadEntities(e => e.VisitTime < n && e.CreateUserId == modela.Id).ToList().Count;
                n = n.AddDays(-7);
                ViewData["15ri"] = orders.LoadEntities(e => e.VisitTime < n && e.CreateUserId == modela.Id).ToList().Count;
            }
            ViewData["User"] = modela;
            #endregion

            string CName = Request["CName"];
            ViewData["type"] = type;
            return View();
        }

        public ActionResult GetOrderPage()
        {
            var pageindex = 1;
            if (!int.TryParse(Request["pageindex"], out pageindex))
            {
                return Content(JsonConvert.SerializeObject(new { success = false }), APPJSON);
            }
            var type = Request["type"];
            //if (string.IsNullOrEmpty(type))
            //{
            //    return Content(JsonConvert.SerializeObject(new { success = false, reload = "reload" }), APPJSON);
            //}

            var temp = orders.LoadEntities(e => true);
            var pagesize = 10;
            var model = (AdminInfo)Session["User"];
            if (model.IsAdmin == false)
            {
                temp = temp.Where(e => e.CreateUserId == model.Id);
            }
            var now = DateTime.Now.Date;
            switch (type)
            {
                case "1":
                    temp = temp.Where(e => e.VisitTime == now);
                    break;
                case "2":
                    now = DateTime.Now.Date.AddDays(-3);
                    temp = temp.Where(e => e.VisitTime < now);
                    break;
                case "3":
                    now = DateTime.Now.Date.AddDays(-7);
                    temp = temp.Where(e => e.VisitTime < now);
                    break;
                case "4":
                    now = DateTime.Now.Date.AddDays(-15);
                    temp = temp.Where(e => e.VisitTime < now);
                    break;
                case "5":
                    temp = temp.Where(e => e.CreateTime.Year == now.Year && e.CreateTime.Month == now.Month && e.CreateTime.Day == now.Day);
                    break;
                case "6":
                    temp = temp.Where(e => e.CreateTime.Month == now.Month && e.CreateTime.Year == now.Year);
                    break;
            }
            DateTime d;
            if (DateTime.TryParse(Request["CreateTime"], out d))
            {
                d = d.Date;
                temp = temp.Where(e => e.CreateTime.Year == d.Year && e.CreateTime.Month == d.Month && e.CreateTime.Day == d.Day);
            }
            string CName = Request["CName"];
            if (!string.IsNullOrEmpty(CName))
            {
                temp = temp.Where(e => e.CustomerName.Contains(CName));
            }
            string Phone = Request["Phone"];
            if (!string.IsNullOrEmpty(Phone))
            {
                temp = temp.Where(e => e.Phone.Contains(Phone));
            }
            string Stars = Request["Stars"];
            if (!string.IsNullOrEmpty(Stars) && Stars != "0")
            {
                temp = temp.Where(e => e.Stars == Stars);
            }
            var totalcount = temp.Count();
            var pagecount = (totalcount + pagesize - 1) / pagesize;
            var list = temp.OrderBy(e => e.Id).Skip((pageindex - 1) * pagesize).Take(pagesize).ToList().Select(e => new
            {
                Id = e.Id,
                CustomerName = e.CustomerName,
                Phone = e.Phone,
                Sex = e.Sex == true ? "男" : "女",
                Age = e.Age,
                GoodName = e.GoodName,
                ExpressNumber = e.ExpressNumber == null ? "" : e.ExpressNumber,
                ByWeChat = e.ByWeChat,
                PayWay = e.PayWay,
                VisitTime = e.VisitTime.ToString("yyyy-MM-dd"),
                Stars = e.Stars,
                Price = e.Price
            });

            return Content(JsonConvert.SerializeObject(new { success = true, totalcount = totalcount, list = list, pagecount = pagecount }), APPJSON);
        }


        public ActionResult GetMoneyPage()
        {
            var pageindex = 1;
            if (!int.TryParse(Request["pageindex"], out pageindex))
            {
                return Content(JsonConvert.SerializeObject(new { success = false }), APPJSON);
            }

            var temp = userservice.LoadEntities(e => true);
            var pagesize = 10;
            var model = (AdminInfo)Session["User"];
            if (model.IsAdmin == false)
            {
                temp = temp.Where(e => e.CreateUserId == model.Id);
            }
            string CName = Request["CName"];
            if (!string.IsNullOrEmpty(CName))
            {
                temp = temp.Where(e => e.UserName.Contains(CName));
            }
            var totalcount = temp.Count();
            var pagecount = (totalcount + pagesize - 1) / pagesize;
            var now = DateTime.Now.Date;
            var list = temp.OrderBy(e => e.Id).Skip((pageindex - 1) * pagesize).Take(pagesize).ToList().Select(e => new
            {
                Name = e.UserName,
                Count = e.Order.Count(s => s.CreateTime.Month == now.Month && s.CreateTime.Year == now.Year),
                Money = GetZongMoney(e.Order.Where(s => s.CreateTime.Month == now.Month && s.CreateTime.Year == now.Year).ToList()),
                VCount = vservice.LoadEntities(a => a.CreateUserId == e.Id && a.CreateTime.Year == now.Year && a.CreateTime.Month == now.Month).Count()
            });

            return Content(JsonConvert.SerializeObject(new { success = true, totalcount = totalcount, list = list, pagecount = pagecount }), APPJSON);
        }

        public decimal? GetZongMoney(List<Order> list)
        {
            decimal? a = 0;
            foreach (var item in list)
            {
                if (item.Price != null)
                {
                    a += item.Price;
                }
            }
            return a;
        }

        public ActionResult ExportExcel()
        {
            try
            {
                var temp = orders.LoadEntities(e => true);
                var model = (AdminInfo)Session["User"];
                string title = "";
                if (model.IsAdmin == false)
                {
                    temp = temp.Where(e => e.CreateUserId == model.Id);
                }
                DateTime d;
                if (DateTime.TryParse(Request["CreateTime"], out d))
                {
                    d = d.Date;
                    temp = temp.Where(e => e.CreateTime == d);
                }
                string CName = Request["CName"];
                if (!string.IsNullOrEmpty(CName))
                {
                    temp = temp.Where(e => e.CustomerName.Contains(CName));
                }
                string Phone = Request["Phone"];
                if (!string.IsNullOrEmpty(Phone))
                {
                    temp = temp.Where(e => e.Phone.Contains(Phone));
                }
                var list = temp.ToList();
                if (model.IsAdmin == true)
                {
                    if (Request["type"] == "0")
                    {
                        title = "根据条件筛选的所有订单";
                    }
                    else if (Request["type"] == "6")
                    {
                        title = "根据条件筛选的当月订单";
                    }
                }
                else
                {
                    if (Request["type"] == "0")
                    {
                        title = "根据条件筛选的所有订单:" + model.UserName;
                    }
                    else if (Request["type"] == "6")
                    {
                        title = "根据条件筛选的当月订单:" + model.UserName;
                    }
                }
                Workbook workbook = new Workbook();
                Worksheet sheet = workbook.Worksheets[0];
                Cells cells = sheet.Cells;//单元格

                //为标题设置样式    
                Style styleTitle = workbook.Styles[workbook.Styles.Add()];//新增样式
                styleTitle.HorizontalAlignment = TextAlignmentType.Center;//文字居中
                styleTitle.Font.Name = "宋体";//文字字体
                styleTitle.Font.Size = 18;//文字大小
                styleTitle.Font.IsBold = true;//粗体

                //样式2
                Style style2 = workbook.Styles[workbook.Styles.Add()];//新增样式
                style2.HorizontalAlignment = TextAlignmentType.Center;//文字居中
                style2.Font.Name = "宋体";//文字字体
                style2.Font.Size = 14;//文字大小
                style2.Font.IsBold = true;//粗体
                style2.IsTextWrapped = true;//单元格内容自动换行

                //样式3
                Style style3 = workbook.Styles[workbook.Styles.Add()];//新增样式
                style3.HorizontalAlignment = TextAlignmentType.Center;//文字居中
                style3.Font.Name = "宋体";//文字字体
                style3.Font.Size = 12;//文字大小

                int Colnum = 12;//表格列数
                int Rownum = list.Count;//表格行数

                //生成行1 标题行   
                cells.Merge(0, 0, 1, Colnum);//合并单元格
                cells[0, 0].PutValue(title);//填写内容
                cells[0, 0].SetStyle(styleTitle);
                cells.SetRowHeight(0, 45);

                cells[1, 0].PutValue("序号");
                cells[1, 0].SetStyle(style2);
                cells.SetColumnWidth(0, 3);

                cells[1, 1].PutValue("客户姓名");
                cells[1, 1].SetStyle(style2);
                cells.SetColumnWidth(0, 3);

                cells[1, 2].PutValue("手机号码");
                cells[1, 2].SetStyle(style2);
                cells.SetColumnWidth(1, 30);

                cells[1, 3].PutValue("性别");
                cells[1, 3].SetStyle(style2);
                cells.SetColumnWidth(2, 5);

                cells[1, 4].PutValue("年龄");
                cells[1, 4].SetStyle(style2);
                cells.SetColumnWidth(3, 5);

                cells[1, 5].PutValue("服务人员");
                cells[1, 5].SetStyle(style2);
                cells.SetColumnWidth(3, 10);


                cells[1, 6].PutValue("客户地址");
                cells[1, 6].SetStyle(style2);
                cells.SetColumnWidth(4, 15);

                cells[1, 7].PutValue("详情");
                cells[1, 7].SetStyle(style2);
                cells.SetColumnWidth(5, 100);


                cells[1, 8].PutValue("产品名称");
                cells[1, 8].SetStyle(style2);
                cells.SetColumnWidth(6, 15);

                cells[1, 9].PutValue("订购微信");
                cells[1, 9].SetStyle(style2);
                cells.SetColumnWidth(7, 15);

                cells[1, 10].PutValue("支付方式");
                cells[1, 10].SetStyle(style2);
                cells.SetColumnWidth(8, 15);

                cells[1, 11].PutValue("最后一次回访时间");
                cells[1, 11].SetStyle(style2);
                cells.SetColumnWidth(9, 15);

                cells[1, 12].PutValue("价格");
                cells[1, 12].SetStyle(style2);
                cells.SetColumnWidth(10, 25);

                cells[1, 13].PutValue("创建时间");
                cells[1, 13].SetStyle(style2);
                cells.SetColumnWidth(11, 25);

                cells[1, 14].PutValue("快递单号");
                cells[1, 14].SetStyle(style2);
                cells.SetColumnWidth(11, 25);

                cells[1, 15].PutValue("订单等级");
                cells[1, 15].SetStyle(style2);
                cells.SetColumnWidth(11, 25);
                //if (result.Count != 0)
                //{
                //    for (int i = 0; i < result.Count; i++)
                //    {
                //        cells[1, 11 + i].PutValue(result[i].FanName);
                //        cells[1, 11 + i].SetStyle(style2);
                //        cells.SetColumnWidth(11 + i, 25);
                //    }
                //}

                ////生成数据行
                for (int i = 0; i < Rownum; i++)
                {
                    cells[2 + i, 0].PutValue(i + 1);
                    cells[2 + i, 0].SetStyle(style3);

                    cells[2 + i, 1].PutValue(list[i].CustomerName);
                    cells[2 + i, 1].SetStyle(style3);

                    cells[2 + i, 2].PutValue(list[i].Phone);
                    cells[2 + i, 2].SetStyle(style3);

                    if (list[i].Sex == true)
                    {
                        cells[2 + i, 3].PutValue("男");
                        cells[2 + i, 3].SetStyle(style3);
                    }
                    else
                    {
                        cells[2 + i, 3].PutValue("女");
                        cells[2 + i, 3].SetStyle(style3);
                    }


                    cells[2 + i, 4].PutValue(list[i].Age);
                    cells[2 + i, 4].SetStyle(style3);

                    cells[2 + i, 5].PutValue(list[i].ByName);
                    cells[2 + i, 5].SetStyle(style3);

                    cells[2 + i, 6].PutValue(list[i].Address);
                    cells[2 + i, 6].SetStyle(style3);

                    cells[2 + i, 7].PutValue(list[i].Details);
                    cells[2 + i, 7].SetStyle(style3);

                    cells[2 + i, 8].PutValue(list[i].GoodName);
                    cells[2 + i, 8].SetStyle(style3);

                    cells[2 + i, 9].PutValue(list[i].ByWeChat);
                    cells[2 + i, 9].SetStyle(style3);

                    cells[2 + i, 10].PutValue(list[i].PayWay);
                    cells[2 + i, 10].SetStyle(style3);

                    cells[2 + i, 11].PutValue(list[i].VisitTime.ToString("yyyy-MM-dd"));
                    cells[2 + i, 11].SetStyle(style3);

                    cells[2 + i, 12].PutValue(list[i].Price);
                    cells[2 + i, 12].SetStyle(style3);

                    cells[2 + i, 13].PutValue(list[i].CreateTime.ToString("yyyy-MM-dd"));
                    cells[2 + i, 13].SetStyle(style3);

                    cells[2 + i, 14].PutValue(list[i].ExpressNumber == null ? "" : list[i].ExpressNumber);
                    cells[2 + i, 14].SetStyle(style3);
                    cells.SetRowHeight(3 + i, 24);

                    cells[2 + i, 15].PutValue(list[i].Stars);
                    cells[2 + i, 15].SetStyle(style3);
                    cells.SetRowHeight(3 + i, 24);
                }
                string filename = DateTime.Now.ToString("yyyyMMddhhmmss") + model.UserName;
                filename = "/ExportDoc/" + filename + ".xls";
                workbook.Save(Server.MapPath(filename));
                return Content(JsonConvert.SerializeObject(new { success = true, url = filename }), APPJSON);
            }
            catch (Exception ex)
            {
                return Content(JsonConvert.SerializeObject(new { success = false, msg = ex.Message, res = ex.ToString() }), APPJSON);
            }
        }

        public ActionResult AddOrder()
        {
            if (Session["User"] == null)
            {
                return View("Index");
            }
            #region 数字
            var modela = (AdminInfo)Session["User"];
            var n = DateTime.Now.Date;
            if (modela.IsAdmin == true)
            {
                ViewData["jinridingdan"] = orders.LoadEntities(e => e.CreateTime.Year == n.Year && e.CreateTime.Month == n.Month && e.CreateTime.Day == n.Day).Count();
                ViewData["jinri"] = orders.LoadEntities(e => e.VisitTime == n).Count();
                n = n.AddDays(-3);
                ViewData["3ri"] = orders.LoadEntities(e => e.VisitTime < n).Count();
                n = n.AddDays(-4);
                ViewData["7ri"] = orders.LoadEntities(e => e.VisitTime < n).Count();
                n = n.AddDays(-7);
                ViewData["15ri"] = orders.LoadEntities(e => e.VisitTime < n).Count();
            }
            else
            {
                ViewData["jinridingdan"] = orders.LoadEntities(e => e.CreateUserId == modela.Id && e.CreateTime.Year == n.Year && e.CreateTime.Month == n.Month && e.CreateTime.Day == n.Day).Count();
                ViewData["jinri"] = orders.LoadEntities(e => e.VisitTime == n && e.CreateUserId == modela.Id).ToList().Count;
                n = n.AddDays(-3);
                ViewData["3ri"] = orders.LoadEntities(e => e.VisitTime < n && e.CreateUserId == modela.Id).ToList().Count;
                n = n.AddDays(-4);
                ViewData["7ri"] = orders.LoadEntities(e => e.VisitTime < n && e.CreateUserId == modela.Id).ToList().Count;
                n = n.AddDays(-7);
                ViewData["15ri"] = orders.LoadEntities(e => e.VisitTime < n && e.CreateUserId == modela.Id).ToList().Count;
            }
            ViewData["User"] = modela;
            ViewData["UserIdForAdd"] = modela.Id;
            int id;
            if (int.TryParse(Request["CustomerId"], out id))
            {
                var model = cservice.LoadEntities(e => e.Id == id).FirstOrDefault();
                if (model != null)
                {
                    return View(model);
                }
            }
            #endregion
            return View(new Customer());
        }

        public ActionResult AddOrderOn(Order order)
        {

            try
            {
                if (order.Id == 0)
                {
                    order.CreateTime = DateTime.Now;
                    order.CreateUserId = Convert.ToInt32(Request["UserIdForAdd"]);

                    var cmodel = cservice.LoadEntities(e => e.Phone == order.Phone).FirstOrDefault();
                    if (cmodel == null)
                    {
                        cmodel = new Customer();
                        cmodel.Name = order.CustomerName;
                        cmodel.Phone = order.Phone;
                        cmodel.Age = order.Age;
                        cmodel.Stars = "暂无";
                        cmodel.Sex = order.Sex;
                        cmodel.Address = order.Address;
                        cmodel.Stars = order.Stars;
                        cmodel.Whos = Convert.ToInt32(Request["UserIdForAdd"]);
                        cmodel.Details = order.Details;
                        cservice.AddEntity(cmodel);
                        order.CustomerId = cservice.LoadEntities(e => e.Phone == cmodel.Phone && e.Name == cmodel.Name).First().Id;
                    }
                    else
                    {
                        order.CustomerId = cmodel.Id;
                        cmodel.Name = order.CustomerName;
                        cmodel.Phone = order.Phone;
                        cmodel.Age = order.Age;
                        cmodel.Stars = "暂无";
                        cmodel.Sex = order.Sex;
                        cmodel.Address = order.Address;
                        cmodel.Stars = order.Stars;
                        cmodel.Whos = order.CreateUserId;
                        cmodel.Details = order.Details;
                        cservice.EditEntity(cmodel);
                        order.CustomerId = cmodel.Id;
                    }

                    orders.AddEntity(order);
                    return Content(JsonConvert.SerializeObject(new { success = true, msg = "成功" }), APPJSON);
                }
                else
                {
                    var cmodel = cservice.LoadEntities(e => e.Phone == order.Phone).FirstOrDefault();
                    if (cmodel == null)
                    {
                        cmodel = new Customer();
                        cmodel.Name = order.CustomerName;
                        cmodel.Phone = order.Phone;
                        cmodel.Age = order.Age;
                        cmodel.Stars = order.Stars;
                        cmodel.Sex = order.Sex;
                        cmodel.Address = order.Address;
                        cmodel.Stars = order.Stars;
                        cmodel.Whos = Convert.ToInt32(Session["UserId"]);
                        cmodel.Details = order.Details;
                        cservice.AddEntity(cmodel);
                        order.CustomerId = cservice.LoadEntities(e => e.Phone == cmodel.Phone && e.Name == cmodel.Name).First().Id;
                    }
                    else
                    {
                        order.CustomerId = cmodel.Id;
                        cmodel.Name = order.CustomerName;
                        cmodel.Phone = order.Phone;
                        cmodel.Age = order.Age;
                        cmodel.Stars = order.Stars;
                        cmodel.Sex = order.Sex;
                        cmodel.Address = order.Address;
                        cmodel.Stars = order.Stars;
                        cmodel.Whos = Convert.ToInt32(Session["UserId"]);
                        cmodel.Details = order.Details;
                        order.CustomerId = cmodel.Id;
                        cservice.EditEntity(cmodel);
                    }
                    if (orders.EditEntity(order))
                    {
                        return Content(JsonConvert.SerializeObject(new { success = true, msg = "成功" }), APPJSON);
                    }
                    else
                    {
                        return Content(JsonConvert.SerializeObject(new { success = false, msg = "修改失败" }), APPJSON);
                    }
                }

            }
            catch (Exception ex)
            {
                string msg = ex.Message;
                if (msg.Contains("实体的验证失败"))
                {
                    return Content(JsonConvert.SerializeObject(new { success = false, requied = true, msg = "系统异常请重新登陆！" }), APPJSON);
                }
                return Content(JsonConvert.SerializeObject(new { success = false, msg = "系统异常请重新登陆！" }), APPJSON);
            }
        }

        public ActionResult GetOrderById(int id = 0)
        {
            if (Session["User"] == null)
            {
                return View("Index");
            }
            #region 数字
            var modela = (AdminInfo)Session["User"];
            var n = DateTime.Now.Date;
            if (modela.IsAdmin == true)
            {
                ViewData["jinridingdan"] = orders.LoadEntities(e => e.CreateTime.Year == n.Year && e.CreateTime.Month == n.Month && e.CreateTime.Day == n.Day).Count();
                ViewData["jinri"] = orders.LoadEntities(e => e.VisitTime == n).Count();
                n = n.AddDays(-3);
                ViewData["3ri"] = orders.LoadEntities(e => e.VisitTime < n).Count();
                n = n.AddDays(-4);
                ViewData["7ri"] = orders.LoadEntities(e => e.VisitTime < n).Count();
                n = n.AddDays(-7);
                ViewData["15ri"] = orders.LoadEntities(e => e.VisitTime < n).Count();
            }
            else
            {
                ViewData["jinridingdan"] = orders.LoadEntities(e => e.CreateUserId == modela.Id && e.CreateTime.Year == n.Year && e.CreateTime.Month == n.Month && e.CreateTime.Day == n.Day).Count();
                ViewData["jinri"] = orders.LoadEntities(e => e.VisitTime == n && e.CreateUserId == modela.Id).Count();
                n = n.AddDays(-3);
                ViewData["3ri"] = orders.LoadEntities(e => e.VisitTime < n && e.CreateUserId == modela.Id).Count();
                n = n.AddDays(-4);
                ViewData["7ri"] = orders.LoadEntities(e => e.VisitTime < n && e.CreateUserId == modela.Id).Count();
                n = n.AddDays(-7);
                ViewData["15ri"] = orders.LoadEntities(e => e.VisitTime < n && e.CreateUserId == modela.Id).Count();
            }
            ViewData["User"] = modela;
            #endregion
            var order = orders.LoadEntities(e => e.Id == id).FirstOrDefault();
            if (order == null)
            {
                return View("Main");
            }
            return View(order);
        }

        public ActionResult AddVisit(int OrderId)
        {
            if (Session["User"] == null)
            {
                return View("Index");
            }
            #region 数字
            var modela = (AdminInfo)Session["User"];
            var n = DateTime.Now.Date;
            if (modela.IsAdmin == true)
            {
                ViewData["jinridingdan"] = orders.LoadEntities(e => e.CreateTime.Year == n.Year && e.CreateTime.Month == n.Month && e.CreateTime.Day == n.Day).Count();
                ViewData["jinri"] = orders.LoadEntities(e => e.VisitTime == n).Count();
                n = n.AddDays(-3);
                ViewData["3ri"] = orders.LoadEntities(e => e.VisitTime < n).Count();
                n = n.AddDays(-4);
                ViewData["7ri"] = orders.LoadEntities(e => e.VisitTime < n).Count();
                n = n.AddDays(-7);
                ViewData["15ri"] = orders.LoadEntities(e => e.VisitTime < n).Count();
            }
            else
            {
                ViewData["jinridingdan"] = orders.LoadEntities(e => e.CreateUserId == modela.Id && e.CreateTime.Year == n.Year && e.CreateTime.Month == n.Month && e.CreateTime.Day == n.Day).Count();
                ViewData["jinri"] = orders.LoadEntities(e => e.VisitTime == n && e.CreateUserId == modela.Id).ToList().Count;
                n = n.AddDays(-3);
                ViewData["3ri"] = orders.LoadEntities(e => e.VisitTime < n && e.CreateUserId == modela.Id).ToList().Count;
                n = n.AddDays(-4);
                ViewData["7ri"] = orders.LoadEntities(e => e.VisitTime < n && e.CreateUserId == modela.Id).ToList().Count;
                n = n.AddDays(-7);
                ViewData["15ri"] = orders.LoadEntities(e => e.VisitTime < n && e.CreateUserId == modela.Id).ToList().Count;
            }
            ViewData["User"] = modela;
            int id;
            if (int.TryParse(Request["CustomerId"], out id))
            {
                var model = cservice.LoadEntities(e => e.Id == id).FirstOrDefault();
                if (model != null)
                {
                    return View(model);
                }
            }
            #endregion
            ViewData["OrderId"] = OrderId;
            var order = orders.LoadEntities(e => e.Id == OrderId).FirstOrDefault();
            ViewData["Stars"] = cservice.LoadEntities(e => e.Id == order.CustomerId).FirstOrDefault().Stars;
            return View();
        }

        public ActionResult AddVisitOn(Visit visit)
        {
            int orderid;
            if (!int.TryParse(Request["OrderId"], out orderid))
            {
                return Content(JsonConvert.SerializeObject(new { success = false, msg = "数据异常" }), APPJSON);
            }
            DateTime ntime;
            if (!DateTime.TryParse(Request["VisitTime"], out ntime))
            {
                return Content(JsonConvert.SerializeObject(new { success = false, msg = "请选择下次回访日期" }), APPJSON);
            }
            string details = Request["Details"];
            if (string.IsNullOrEmpty(details))
            {
                return Content(JsonConvert.SerializeObject(new { success = false, msg = "请填写回访详情" }), APPJSON);
            }
            var order = orders.LoadEntities(e => e.Id == orderid).FirstOrDefault();
            if (order == null)
            {
                return Content(JsonConvert.SerializeObject(new { success = false, msg = "数据异常" }), APPJSON);
            }
            order.VisitTime = ntime.Date;
            var cmodel = cservice.LoadEntities(e => e.Id == order.CustomerId).FirstOrDefault();
            cmodel.Stars = Request["Stars"];
            if (!orders.EditEntity(order))
            {
                return Content(JsonConvert.SerializeObject(new { success = false, msg = "数据异常" }), APPJSON);
            }

            visit.OrderId = orderid;
            visit.Details = details;
            visit.CreateTime = DateTime.Now;
            visit.VisitTime = DateTime.Now.Date;
            visit.CreateUserId = Convert.ToInt32(Session["UserId"]);
            vservice.AddEntity(visit);
            return Content(JsonConvert.SerializeObject(new { success = true, msg = "成功" }), APPJSON);
        }

        public ActionResult GetOrderByIdToUpdate(int id = 0)
        {
            if (Session["User"] == null)
            {
                return View("Index");
            }
            #region 数字
            var modela = (AdminInfo)Session["User"];
            var n = DateTime.Now.Date;
            if (modela.IsAdmin == true)
            {
                ViewData["jinridingdan"] = orders.LoadEntities(e => e.CreateTime.Year == n.Year && e.CreateTime.Month == n.Month && e.CreateTime.Day == n.Day).Count();
                ViewData["jinri"] = orders.LoadEntities(e => e.VisitTime == n).Count();
                n = n.AddDays(-3);
                ViewData["3ri"] = orders.LoadEntities(e => e.VisitTime < n).Count();
                n = n.AddDays(-4);
                ViewData["7ri"] = orders.LoadEntities(e => e.VisitTime < n).Count();
                n = n.AddDays(-7);
                ViewData["15ri"] = orders.LoadEntities(e => e.VisitTime < n).Count();
            }
            else
            {
                ViewData["jinridingdan"] = orders.LoadEntities(e => e.CreateUserId == modela.Id && e.CreateTime.Year == n.Year && e.CreateTime.Month == n.Month && e.CreateTime.Day == n.Day).Count();
                ViewData["jinri"] = orders.LoadEntities(e => e.VisitTime == n && e.CreateUserId == modela.Id).Count();
                n = n.AddDays(-3);
                ViewData["3ri"] = orders.LoadEntities(e => e.VisitTime < n && e.CreateUserId == modela.Id).Count();
                n = n.AddDays(-4);
                ViewData["7ri"] = orders.LoadEntities(e => e.VisitTime < n && e.CreateUserId == modela.Id).Count();
                n = n.AddDays(-7);
                ViewData["15ri"] = orders.LoadEntities(e => e.VisitTime < n && e.CreateUserId == modela.Id).Count();
            }
            ViewData["User"] = modela;
            #endregion
            var order = orders.LoadEntities(e => e.Id == id).FirstOrDefault();
            if (order == null)
            {
                return View("Main");
            }
            return View(order);
        }

        [HttpPost]
        public ActionResult UploadPic()
        {
            string path = string.Empty;
            HttpFileCollectionBase files = Request.Files;//接收文件
            if (files.Count > 0)
            {
                foreach (string temp_i in files)
                {
                    HttpPostedFileBase file = files[temp_i];
                    string filename = Path.GetFileName(file.FileName);
                    if (string.IsNullOrWhiteSpace(filename))
                    {
                        continue;
                    }
                    string fileext = Path.GetExtension(filename);
                    if (fileext.Contains("exe") || fileext.Contains("msi") || fileext.Contains("bat") || fileext.Contains("com") || fileext.Contains("sys"))
                    {
                        throw new Exception("上传文件中包含危险文件!");
                    }
                    if (!Directory.Exists(Server.MapPath("/ImageFiles/")))
                    {
                        Directory.CreateDirectory(Server.MapPath("/ImageFiles/"));
                    }

                    string dircstr = "/ImageFiles/" + Request["path"] + "/";
                    if (!Directory.Exists(Server.MapPath(dircstr)))
                    {
                        Directory.CreateDirectory(Path.GetDirectoryName(Server.MapPath(dircstr)));
                    }
                    string fileloadname = dircstr + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + Path.GetRandomFileName() + fileext;

                    file.SaveAs(Server.MapPath(fileloadname));
                    //Image image = Image.FromFile(Server.MapPath(fileloadname));
                    //压缩小图标
                    //Image imageNew = ImageCutter.ImgReduceCutOut(image, 300, 300, false);

                    string fileloadname_s = fileloadname.Replace(fileext, "_s" + fileext);
                    //imageNew.Save(Server.MapPath(fileloadname_s));
                    path += fileloadname + ',';
                }
            }
            path = path.TrimEnd(',');
            return Content(JsonConvert.SerializeObject(new { code = 0, msg = "成功", data = path }), APPJSON);
            //return Content(JsonConvert.SerializeObject(new { success = true, url = path }));
        }

        public ActionResult AddUser()
        {
            if (Session["User"] == null)
            {
                return View("Index");
            }
            #region 数字
            var modela = (AdminInfo)Session["User"];
            var n = DateTime.Now.Date;
            if (modela.IsAdmin == true)
            {
                ViewData["jinridingdan"] = orders.LoadEntities(e => e.CreateTime.Year == n.Year && e.CreateTime.Month == n.Month && e.CreateTime.Day == n.Day).Count();
                ViewData["jinri"] = orders.LoadEntities(e => e.VisitTime == n).Count();
                n = n.AddDays(-3);
                ViewData["3ri"] = orders.LoadEntities(e => e.VisitTime < n).Count();
                n = n.AddDays(-4);
                ViewData["7ri"] = orders.LoadEntities(e => e.VisitTime < n).Count();
                n = n.AddDays(-7);
                ViewData["15ri"] = orders.LoadEntities(e => e.VisitTime < n).Count();
            }
            else
            {
                ViewData["jinridingdan"] = orders.LoadEntities(e => e.CreateUserId == modela.Id && e.CreateTime.Year == n.Year && e.CreateTime.Month == n.Month && e.CreateTime.Day == n.Day).Count();
                ViewData["jinri"] = orders.LoadEntities(e => e.VisitTime == n && e.CreateUserId == modela.Id).ToList().Count;
                n = n.AddDays(-3);
                ViewData["3ri"] = orders.LoadEntities(e => e.VisitTime < n && e.CreateUserId == modela.Id).ToList().Count;
                n = n.AddDays(-4);
                ViewData["7ri"] = orders.LoadEntities(e => e.VisitTime < n && e.CreateUserId == modela.Id).ToList().Count;
                n = n.AddDays(-7);
                ViewData["15ri"] = orders.LoadEntities(e => e.VisitTime < n && e.CreateUserId == modela.Id).ToList().Count;
            }
            ViewData["User"] = modela;
            #endregion
            return View();
        }

        public ActionResult AddUserOn()
        {
            if (Session["User"] == null)
            {
                return Content(JsonConvert.SerializeObject(new { success = false, msg = "系统异常请重新登陆！" }), APPJSON);
            }
            string Account = Request["Account"];
            if (string.IsNullOrEmpty(Account))
            {
                return Content(JsonConvert.SerializeObject(new { success = false, msg = "登录名不能为空！" }), APPJSON);
            }
            string PassWord = Request["Password"];
            if (string.IsNullOrEmpty(PassWord))
            {
                return Content(JsonConvert.SerializeObject(new { success = false, msg = "密码不能为空！" }), APPJSON);
            }
            string UserName = Request["UserName"];
            if (string.IsNullOrEmpty(UserName))
            {
                return Content(JsonConvert.SerializeObject(new { success = false, msg = "用户名不能为空！" }), APPJSON);
            }
            bool IsAdmin;
            if (!bool.TryParse(Request["IsAdmin"], out IsAdmin))
            {
                return Content(JsonConvert.SerializeObject(new { success = false, msg = "数据异常！" }), APPJSON);
            }

            string id = Request["Id"];
            if (string.IsNullOrEmpty(id))
            {
                if (userservice.LoadEntities(e => e.Account == Account).Count() > 0)
                {
                    return Content(JsonConvert.SerializeObject(new { success = false, msg = "该账号以被占用！" }), APPJSON);
                }
                AdminInfo user = new AdminInfo();
                user.Account = Account;
                user.PassWord = PassWord;
                user.UserName = UserName;
                user.IsAdmin = IsAdmin;
                user.CreateTime = DateTime.Now;
                user.LastLoginTime = DateTime.Now;
                user.State = 1;
                user.CreateUserId = Convert.ToInt32(Session["UserId"]);

                if (userservice.AddEntity(user) != null)
                {
                    return Content(JsonConvert.SerializeObject(new { success = true, msg = "成功！" }), APPJSON);
                }
                else
                {
                    return Content(JsonConvert.SerializeObject(new { success = false, msg = "添加失败请联系管理员！" }), APPJSON);

                }
            }
            else
            {

                var uid = Convert.ToInt32(id);
                var model = userservice.LoadEntities(e => e.Id == uid).FirstOrDefault();
                if (model == null)
                {
                    return Content(JsonConvert.SerializeObject(new { success = false, msg = "数据异常！" }), APPJSON);
                }
                var MasterAccount = ConfigurationManager.AppSettings["MasterAccount"];
                if (model.Account == MasterAccount)
                {
                    return Content(JsonConvert.SerializeObject(new { success = false, msg = "最高权限账号不可修改！" }), APPJSON);
                }
                if (userservice.LoadEntities(e => e.Account == Account).Count() > 0 && model.Account != Account)
                {
                    return Content(JsonConvert.SerializeObject(new { success = false, msg = "该账号以被占用！" }), APPJSON);
                }
                model.Account = Account;
                model.PassWord = PassWord;
                model.IsAdmin = IsAdmin;
                model.UserName = UserName;
                if (userservice.EditEntity(model))
                {
                    return Content(JsonConvert.SerializeObject(new { success = true, msg = "成功！" }), APPJSON);
                }
                else
                {
                    return Content(JsonConvert.SerializeObject(new { success = false, msg = "修改失败！" }), APPJSON);
                }
            }

        }

        public ActionResult ThisMonthMoney()
        {
            #region 数字
            var modela = (AdminInfo)Session["User"];
            var n = DateTime.Now.Date;
            ViewData["MoneySum"] = 0;
            if (orders.LoadEntities(e => e.CreateTime.Year == n.Year && e.CreateTime.Month == n.Month).Count() > 0)
            {
                ViewData["MoneySum"] = orders.LoadEntities(e => e.CreateTime.Year == n.Year && e.CreateTime.Month == n.Month).Sum(e => e.Price).ToString("C");
            }
            if (modela.IsAdmin == true)
            {
                ViewData["jinridingdan"] = orders.LoadEntities(e => e.CreateTime.Year == n.Year && e.CreateTime.Month == n.Month && e.CreateTime.Day == n.Day).Count();
                ViewData["jinri"] = orders.LoadEntities(e => e.VisitTime == n).Count();
                n = n.AddDays(-3);
                ViewData["3ri"] = orders.LoadEntities(e => e.VisitTime < n).Count();
                n = n.AddDays(-4);
                ViewData["7ri"] = orders.LoadEntities(e => e.VisitTime < n).Count();
                n = n.AddDays(-7);
                ViewData["15ri"] = orders.LoadEntities(e => e.VisitTime < n).Count();
            }
            else
            {
                ViewData["jinridingdan"] = orders.LoadEntities(e => e.CreateUserId == modela.Id && e.CreateTime.Year == n.Year && e.CreateTime.Month == n.Month && e.CreateTime.Day == n.Day).Count();
                ViewData["jinri"] = orders.LoadEntities(e => e.VisitTime == n && e.CreateUserId == modela.Id).Count();
                n = n.AddDays(-3);
                ViewData["3ri"] = orders.LoadEntities(e => e.VisitTime < n && e.CreateUserId == modela.Id).Count();
                n = n.AddDays(-4);
                ViewData["7ri"] = orders.LoadEntities(e => e.VisitTime < n && e.CreateUserId == modela.Id).Count();
                n = n.AddDays(-7);
                ViewData["15ri"] = orders.LoadEntities(e => e.VisitTime < n && e.CreateUserId == modela.Id).Count();
            }
            ViewData["User"] = modela;


            #endregion
            return View();
        }

        public ActionResult ExportExcelMonthMoney()
        {
            try
            {
                var temp = userservice.LoadEntities(e => true);
                DateTime d;
                if (DateTime.TryParse(Request["CreateTime"], out d))
                {
                    d = d.Date;
                    temp = temp.Where(e => e.CreateTime == d);
                }
                string CName = Request["CName"];
                if (!string.IsNullOrEmpty(CName))
                {
                    temp = temp.Where(e => e.UserName.Contains(CName));
                }
                var list = temp.ToList();

                Workbook workbook = new Workbook();
                Worksheet sheet = workbook.Worksheets[0];
                Cells cells = sheet.Cells;//单元格

                //为标题设置样式    
                Style styleTitle = workbook.Styles[workbook.Styles.Add()];//新增样式
                styleTitle.HorizontalAlignment = TextAlignmentType.Center;//文字居中
                styleTitle.Font.Name = "宋体";//文字字体
                styleTitle.Font.Size = 18;//文字大小
                styleTitle.Font.IsBold = true;//粗体

                //样式2
                Style style2 = workbook.Styles[workbook.Styles.Add()];//新增样式
                style2.HorizontalAlignment = TextAlignmentType.Center;//文字居中
                style2.Font.Name = "宋体";//文字字体
                style2.Font.Size = 14;//文字大小
                style2.Font.IsBold = true;//粗体
                style2.IsTextWrapped = true;//单元格内容自动换行

                //样式3
                Style style3 = workbook.Styles[workbook.Styles.Add()];//新增样式
                style3.HorizontalAlignment = TextAlignmentType.Center;//文字居中
                style3.Font.Name = "宋体";//文字字体
                style3.Font.Size = 12;//文字大小

                int Colnum = 12;//表格列数
                int Rownum = list.Count;//表格行数

                //生成行1 标题行   
                cells.Merge(0, 0, 1, Colnum);//合并单元格
                cells[0, 0].PutValue("截止至" + DateTime.Now.ToString("yyyy-MM-dd") + "工作统计");//填写内容
                cells[0, 0].SetStyle(styleTitle);
                cells.SetRowHeight(0, 45);

                cells[1, 0].PutValue("序号");
                cells[1, 0].SetStyle(style2);
                cells.SetColumnWidth(0, 3);

                cells[1, 1].PutValue("姓名");
                cells[1, 1].SetStyle(style2);
                cells.SetColumnWidth(0, 3);

                cells[1, 2].PutValue("订单数量");
                cells[1, 2].SetStyle(style2);
                cells.SetColumnWidth(1, 30);

                cells[1, 3].PutValue("回访次数");
                cells[1, 3].SetStyle(style2);
                cells.SetColumnWidth(2, 5);

                cells[1, 4].PutValue("售出总额");
                cells[1, 4].SetStyle(style2);
                cells.SetColumnWidth(3, 5);


                //if (result.Count != 0)
                //{
                //    for (int i = 0; i < result.Count; i++)
                //    {
                //        cells[1, 11 + i].PutValue(result[i].FanName);
                //        cells[1, 11 + i].SetStyle(style2);
                //        cells.SetColumnWidth(11 + i, 25);
                //    }
                //}

                ////生成数据行
                for (int i = 0; i < Rownum; i++)
                {
                    cells[2 + i, 0].PutValue(i + 1);
                    cells[2 + i, 0].SetStyle(style3);

                    cells[2 + i, 1].PutValue(list[i].UserName);
                    cells[2 + i, 1].SetStyle(style3);
                    var now = DateTime.Now.Date;
                    var count = list[i].Order.Count(s => s.CreateTime.Month == now.Month && s.CreateTime.Year == now.Year);
                    cells[2 + i, 2].PutValue(count);
                    cells[2 + i, 2].SetStyle(style3);

                    var id = list[i].Id;
                    cells[2 + i, 3].PutValue(vservice.LoadEntities(e => e.CreateUserId == id && e.CreateTime.Year == now.Year && e.CreateTime.Month == now.Month).Count());
                    cells[2 + i, 3].SetStyle(style3);



                    cells[2 + i, 4].PutValue(GetZongMoney(list[i].Order.ToList()));
                    cells[2 + i, 4].SetStyle(style3);



                    cells.SetRowHeight(3 + i, 24);
                }
                string filename = DateTime.Now.ToString("yyyyMMddhhmmss") + "业绩汇报";
                filename = "/ExportDoc/" + filename + ".xls";
                workbook.Save(Server.MapPath(filename));
                return Content(JsonConvert.SerializeObject(new { success = true, url = filename }), APPJSON);
            }
            catch (Exception ex)
            {
                string msg = ex.Message;
                return Content(JsonConvert.SerializeObject(new { success = false }), APPJSON);
            }
        }

        public ActionResult DeleteOrder()
        {
            try
            {
                int orderid;
                if (!int.TryParse(Request["OrderId"], out orderid))
                {
                    return Content(JsonConvert.SerializeObject(new { success = false, msg = "数据异常！" }), APPJSON);
                }
                var order = orders.LoadEntities(e => e.Id == orderid).FirstOrDefault();
                if (order == null)
                {
                    return Content(JsonConvert.SerializeObject(new { success = false, msg = "该订单已被删除！" }), APPJSON);
                }
                var resu = vservice.DeleteMoreEntity(order.Visit.ToList());
                if (orders.DeleteEntity(order))
                {
                    return Content(JsonConvert.SerializeObject(new { success = true, msg = "！" }), APPJSON);
                }
                return Content(JsonConvert.SerializeObject(new { success = false, msg = "删除失败！" }), APPJSON);
            }
            catch (Exception ex)
            {
                return Content(JsonConvert.SerializeObject(new { success = false, msg = "删除失败！" }), APPJSON);
            }
        }

        public ActionResult AdminInfoList()
        {
            if (Session["User"] == null)
            {
                return View("Index");
            }
            #region 数字
            var modela = (AdminInfo)Session["User"];
            var n = DateTime.Now.Date;
            if (modela.IsAdmin == true)
            {
                ViewData["jinridingdan"] = orders.LoadEntities(e => e.CreateTime.Year == n.Year && e.CreateTime.Month == n.Month && e.CreateTime.Day == n.Day).Count();
                ViewData["jinri"] = orders.LoadEntities(e => e.VisitTime == n).Count();
                n = n.AddDays(-3);
                ViewData["3ri"] = orders.LoadEntities(e => e.VisitTime < n).Count();
                n = n.AddDays(-4);
                ViewData["7ri"] = orders.LoadEntities(e => e.VisitTime < n).Count();
                n = n.AddDays(-7);
                ViewData["15ri"] = orders.LoadEntities(e => e.VisitTime < n).Count();
            }
            else
            {
                ViewData["jinridingdan"] = orders.LoadEntities(e => e.CreateUserId == modela.Id && e.CreateTime.Year == n.Year && e.CreateTime.Month == n.Month && e.CreateTime.Day == n.Day).Count();
                ViewData["jinri"] = orders.LoadEntities(e => e.VisitTime == n && e.CreateUserId == modela.Id).Count();
                n = n.AddDays(-3);
                ViewData["3ri"] = orders.LoadEntities(e => e.VisitTime < n && e.CreateUserId == modela.Id).Count();
                n = n.AddDays(-4);
                ViewData["7ri"] = orders.LoadEntities(e => e.VisitTime < n && e.CreateUserId == modela.Id).Count();
                n = n.AddDays(-7);
                ViewData["15ri"] = orders.LoadEntities(e => e.VisitTime < n && e.CreateUserId == modela.Id).Count();
            }
            ViewData["User"] = modela;
            #endregion
            return View();
        }

        public ActionResult CustomerList()
        {
            if (Session["User"] == null)
            {
                return View("Index");
            }
            #region 数字
            var modela = (AdminInfo)Session["User"];
            var n = DateTime.Now.Date;
            if (modela.IsAdmin == true)
            {
                ViewData["jinridingdan"] = orders.LoadEntities(e => e.CreateTime.Year == n.Year && e.CreateTime.Month == n.Month && e.CreateTime.Day == n.Day).Count();
                ViewData["jinri"] = orders.LoadEntities(e => e.VisitTime == n).Count();
                n = n.AddDays(-3);
                ViewData["3ri"] = orders.LoadEntities(e => e.VisitTime < n).Count();
                n = n.AddDays(-4);
                ViewData["7ri"] = orders.LoadEntities(e => e.VisitTime < n).Count();
                n = n.AddDays(-7);
                ViewData["15ri"] = orders.LoadEntities(e => e.VisitTime < n).Count();
            }
            else
            {
                ViewData["jinridingdan"] = orders.LoadEntities(e => e.CreateUserId == modela.Id && e.CreateTime.Year == n.Year && e.CreateTime.Month == n.Month && e.CreateTime.Day == n.Day).Count();
                ViewData["jinri"] = orders.LoadEntities(e => e.VisitTime == n && e.CreateUserId == modela.Id).Count();
                n = n.AddDays(-3);
                ViewData["3ri"] = orders.LoadEntities(e => e.VisitTime < n && e.CreateUserId == modela.Id).Count();
                n = n.AddDays(-4);
                ViewData["7ri"] = orders.LoadEntities(e => e.VisitTime < n && e.CreateUserId == modela.Id).Count();
                n = n.AddDays(-7);
                ViewData["15ri"] = orders.LoadEntities(e => e.VisitTime < n && e.CreateUserId == modela.Id).Count();
            }
            ViewData["User"] = modela;
            #endregion
            return View();
        }

        public ActionResult GetCustomerDetail(int id = -1)
        {
            if (id == -1)
            {
                return View("Main");
            }
            var model = cservice.LoadEntities(e => e.Id == id).FirstOrDefault();
            if (model == null)
            {
                return View("Main");
            }
            if (Session["User"] == null)
            {
                return View("Index");
            }
            #region 数字
            var modela = (AdminInfo)Session["User"];
            var n = DateTime.Now.Date;
            if (modela.IsAdmin == true)
            {
                ViewData["jinridingdan"] = orders.LoadEntities(e => e.CreateTime.Year == n.Year && e.CreateTime.Month == n.Month && e.CreateTime.Day == n.Day).Count();
                ViewData["jinri"] = orders.LoadEntities(e => e.VisitTime == n).Count();
                n = n.AddDays(-3);
                ViewData["3ri"] = orders.LoadEntities(e => e.VisitTime < n).Count();
                n = n.AddDays(-4);
                ViewData["7ri"] = orders.LoadEntities(e => e.VisitTime < n).Count();
                n = n.AddDays(-7);
                ViewData["15ri"] = orders.LoadEntities(e => e.VisitTime < n).Count();
            }
            else
            {
                ViewData["jinridingdan"] = orders.LoadEntities(e => e.CreateUserId == modela.Id && e.CreateTime.Year == n.Year && e.CreateTime.Month == n.Month && e.CreateTime.Day == n.Day).Count();
                ViewData["jinri"] = orders.LoadEntities(e => e.VisitTime == n && e.CreateUserId == modela.Id).Count();
                n = n.AddDays(-3);
                ViewData["3ri"] = orders.LoadEntities(e => e.VisitTime < n && e.CreateUserId == modela.Id).Count();
                n = n.AddDays(-4);
                ViewData["7ri"] = orders.LoadEntities(e => e.VisitTime < n && e.CreateUserId == modela.Id).Count();
                n = n.AddDays(-7);
                ViewData["15ri"] = orders.LoadEntities(e => e.VisitTime < n && e.CreateUserId == modela.Id).Count();
            }
            ViewData["User"] = modela;
            #endregion
            return View(model);
        }

        public ActionResult GetCustomerPage()
        {
            var pageindex = 1;
            if (!int.TryParse(Request["pageindex"], out pageindex))
            {
                return Content(JsonConvert.SerializeObject(new { success = false }), APPJSON);
            }

            var temp = cservice.LoadEntities(e => true);
            var pagesize = 10;
            var model = (AdminInfo)Session["User"];
            if (model.IsAdmin == false)
            {
                temp = temp.Where(e => e.Whos == model.Id);
            }
            string CName = Request["CName"];
            if (!string.IsNullOrEmpty(CName))
            {
                temp = temp.Where(e => e.Name.Contains(CName));
            }
            string Phone = Request["Phone"];
            if (!string.IsNullOrEmpty(Phone))
            {
                temp = temp.Where(e => e.Phone.Contains(Phone));
            }
            var totalcount = temp.Count();
            var pagecount = (totalcount + pagesize - 1) / pagesize;
            var list = temp.OrderBy(e => e.Id).Skip((pageindex - 1) * pagesize).Take(pagesize).ToList().Select(e => new
            {
                Id = e.Id,
                Name = e.Name,
                Phone = e.Phone,
                Sex = e.Sex == true ? "男" : "女",
                Stars = e.Stars,
                Age = e.Age,
                Address = e.Address,
                OrderCount = orders.LoadEntities(s => s.CustomerId == e.Id).Count()
            });

            return Content(JsonConvert.SerializeObject(new { success = true, totalcount = totalcount, list = list, pagecount = pagecount }), APPJSON);
        }

        public ActionResult GetAdminInfoPage()
        {
            var pageindex = 1;
            if (!int.TryParse(Request["pageindex"], out pageindex))
            {
                return Content(JsonConvert.SerializeObject(new { success = false }), APPJSON);
            }

            var temp = userservice.LoadEntities(e => true);
            var pagesize = 10;
            var MasterAccount = ConfigurationManager.AppSettings["MasterAccount"];
            var model = (AdminInfo)Session["User"];
            if (model.Account != MasterAccount)
            {
                temp = temp.Where(e => e.IsAdmin == false);
            }
            string Account = Request["Account"];
            if (!string.IsNullOrEmpty(Account))
            {
                temp = temp.Where(e => e.Account.Contains(Account));
            }
            string Name = Request["Name"];
            if (!string.IsNullOrEmpty(Name))
            {
                temp = temp.Where(e => e.UserName.Contains(Name));
            }
            var totalcount = temp.Count();
            var pagecount = (totalcount + pagesize - 1) / pagesize;
            var list = temp.OrderBy(e => e.Id).Skip((pageindex - 1) * pagesize).Take(pagesize).ToList().Select(e => new
            {
                Id = e.Id,
                Account = e.Account,
                Name = e.UserName,
                IsAdmin = e.IsAdmin == true ? "管理员" : "普通用户",
                State = e.State == 1 ? "正常" : "冻结",
                LastLoginTime = e.LastLoginTime == null ? "" : e.LastLoginTime.ToString("yyyy-MM-dd hh:mm:ss")
            });

            return Content(JsonConvert.SerializeObject(new { success = true, totalcount = totalcount, list = list, pagecount = pagecount }), APPJSON);
        }

        public ActionResult UpdateCustomer(int CustomerId = 0)
        {
            if (Session["User"] == null)
            {
                return View("Index");
            }
            #region 数字
            var modela = (AdminInfo)Session["User"];
            var n = DateTime.Now.Date;
            if (modela.IsAdmin == true)
            {
                ViewData["jinridingdan"] = orders.LoadEntities(e => e.CreateTime.Year == n.Year && e.CreateTime.Month == n.Month && e.CreateTime.Day == n.Day).Count();
                ViewData["jinri"] = orders.LoadEntities(e => e.VisitTime == n).Count();
                n = n.AddDays(-3);
                ViewData["3ri"] = orders.LoadEntities(e => e.VisitTime < n).Count();
                n = n.AddDays(-4);
                ViewData["7ri"] = orders.LoadEntities(e => e.VisitTime < n).Count();
                n = n.AddDays(-7);
                ViewData["15ri"] = orders.LoadEntities(e => e.VisitTime < n).Count();
            }
            else
            {
                ViewData["jinridingdan"] = orders.LoadEntities(e => e.CreateUserId == modela.Id && e.CreateTime.Year == n.Year && e.CreateTime.Month == n.Month && e.CreateTime.Day == n.Day).Count();
                ViewData["jinri"] = orders.LoadEntities(e => e.VisitTime == n && e.CreateUserId == modela.Id).Count();
                n = n.AddDays(-3);
                ViewData["3ri"] = orders.LoadEntities(e => e.VisitTime < n && e.CreateUserId == modela.Id).Count();
                n = n.AddDays(-4);
                ViewData["7ri"] = orders.LoadEntities(e => e.VisitTime < n && e.CreateUserId == modela.Id).Count();
                n = n.AddDays(-7);
                ViewData["15ri"] = orders.LoadEntities(e => e.VisitTime < n && e.CreateUserId == modela.Id).Count();
            }
            ViewData["User"] = modela;
            #endregion
            var order = cservice.LoadEntities(e => e.Id == CustomerId).FirstOrDefault();
            if (order == null)
            {
                return View("Main");
            }
            return View(order);
        }

        public ActionResult DeleteUser()
        {
            try
            {
                int userid;
                if (!int.TryParse(Request["UserId"], out userid))
                {
                    return Content(JsonConvert.SerializeObject(new { success = false, msg = "数据异常！" }), APPJSON);
                }
                var user = userservice.LoadEntities(e => e.Id == userid).FirstOrDefault();
                if (user == null)
                {
                    return Content(JsonConvert.SerializeObject(new { success = false, msg = "数据异常！" }), APPJSON);
                }
                var MasterAccount = ConfigurationManager.AppSettings["MasterAccount"];
                if (MasterAccount == user.Account)
                {
                    return Content(JsonConvert.SerializeObject(new { success = false, msg = "最高权限账号不可冻结！请先更换最高权限账号！" }), APPJSON);
                }
                if (user.State == 1)
                {
                    user.State = -1;
                }
                else
                {
                    user.State = 1;
                }
                if (userservice.EditEntity(user))
                {
                    return Content(JsonConvert.SerializeObject(new { success = true, msg = "操作成功！" }), APPJSON);
                }
                return Content(JsonConvert.SerializeObject(new { success = false, msg = "操作失败！" }), APPJSON);
            }
            catch (Exception ex)
            {
                return Content(JsonConvert.SerializeObject(new { success = false, msg = "删除失败！" }), APPJSON);
            }

        }

        public ActionResult UpdateUser()
        {
            if (Session["User"] == null)
            {
                return View("Index");
            }
            #region 数字
            var modela = (AdminInfo)Session["User"];
            var n = DateTime.Now.Date;
            if (modela.IsAdmin == true)
            {
                ViewData["jinridingdan"] = orders.LoadEntities(e => e.CreateTime.Year == n.Year && e.CreateTime.Month == n.Month && e.CreateTime.Day == n.Day).Count();
                ViewData["jinri"] = orders.LoadEntities(e => e.VisitTime == n).Count();
                n = n.AddDays(-3);
                ViewData["3ri"] = orders.LoadEntities(e => e.VisitTime < n).Count();
                n = n.AddDays(-4);
                ViewData["7ri"] = orders.LoadEntities(e => e.VisitTime < n).Count();
                n = n.AddDays(-7);
                ViewData["15ri"] = orders.LoadEntities(e => e.VisitTime < n).Count();
            }
            else
            {
                ViewData["jinridingdan"] = orders.LoadEntities(e => e.CreateUserId == modela.Id && e.CreateTime.Year == n.Year && e.CreateTime.Month == n.Month && e.CreateTime.Day == n.Day).Count();
                ViewData["jinri"] = orders.LoadEntities(e => e.VisitTime == n && e.CreateUserId == modela.Id).Count();
                n = n.AddDays(-3);
                ViewData["3ri"] = orders.LoadEntities(e => e.VisitTime < n && e.CreateUserId == modela.Id).Count();
                n = n.AddDays(-4);
                ViewData["7ri"] = orders.LoadEntities(e => e.VisitTime < n && e.CreateUserId == modela.Id).Count();
                n = n.AddDays(-7);
                ViewData["15ri"] = orders.LoadEntities(e => e.VisitTime < n && e.CreateUserId == modela.Id).Count();
            }
            ViewData["User"] = modela;
            #endregion
            int id;
            if (!int.TryParse(Request["UserId"], out id))
            {
                return View("Main");
            }
            var model = userservice.LoadEntities(e => e.Id == id).FirstOrDefault();
            if (model == null)
            {
                return View("Main");
            }
            return View(model);
        }

        public ActionResult ModifyPwd()
        {
            return View();
        }
        [HttpPost]
        public ActionResult ModifyPwdOn(string pwd)
        {
            if (string.IsNullOrEmpty(pwd))
            {
                return Content(JsonConvert.SerializeObject(new { success = false, msg = "系统异常请重新登陆！" }), APPJSON);
            }
            var id = Convert.ToInt32(Session["UserId"]);
            var model = userservice.LoadEntities(e => e.Id == id).FirstOrDefault();
            if (model == null)
            {
                return Content(JsonConvert.SerializeObject(new { success = false, msg = "系统异常请重新登陆！" }), APPJSON);
            }
            model.PassWord = pwd;
            if (!userservice.EditEntity(model))
            {
                return Content(JsonConvert.SerializeObject(new { success = false, msg = "系统异常请重新登陆！" }), APPJSON);
            }
            return Content(JsonConvert.SerializeObject(new { success = true }), APPJSON);
        }

        public ActionResult UpdateCustomerOn(Customer customer)
        {
            try
            {
                if (customer == null)
                {
                    return Content(JsonConvert.SerializeObject(new { success = false, msg = "数据异常！" }), APPJSON);
                }
                if (cservice.EditEntity(customer))
                {
                    return Content(JsonConvert.SerializeObject(new { success = true, msg = "" }), APPJSON);
                }
                return Content(JsonConvert.SerializeObject(new { success = false, msg = "修改失败" }), APPJSON);
            }
            catch (Exception ex)
            {
                return Content(JsonConvert.SerializeObject(new { success = false, msg = "数据异常！" }), APPJSON);
            }
            return null;
        }

    }

    public class SummaryViewModel
    {
        public SummaryViewModel()
        {
            TodayFansNum = "-";
            TodayTalkNum = "-";
            NewFansOrderNum = "-";
            OldFansOrderNum = "-";
            SampleOrderNum = "-";
            SampleToforMalOrderNum = "-";
            RePurchaseOrderNum = "-";
            TodaySumNum = "-";
            xfzhl = "-";
            tyzzhl = "-";
            zzl = "-";
            fgl = "-";
            lfzhl = "-";
            ztzhl = "-";
        }

        public string UserName { get; set; }

        public string TodayFansNum { get; set; }
        public string TodayTalkNum { get; set; }
        public string NewFansOrderNum { get; set; }
        public string OldFansOrderNum { get; set; }
        public string SampleOrderNum { get; set; }
        public string SampleToforMalOrderNum { get; set; }

        public string TodaySumNum { get; set; }
        public string RePurchaseOrderNum { get; set; }
        public string xfzhl { get; set; }
        public string tyzzhl { get; set; }
        public string zzl { get; set; }
        public string fgl { get; set; }
        public string lfzhl { get; set; }
        public string ztzhl { get; set; }



    }
}